package com.iss.supermarket.ReturnGoodsUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.entity.Detailed;
import com.iss.supermarket.util.entity.Goods;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ReturnGoodsList extends JFrame {
    private static ReturnGoodsList returnGoodsList;
    private JList<Detailed> jList;
    private ListModelArray<Detailed> listModelArray;
    private JButton jButton1,jButton2;
    private JScrollPane jScrollPane;
    private static JPanel returnGoodsListPanel;

    public ReturnGoodsList(){
        InitUI();
    }

    private void InitUI(){
        Box verticalBox = Box.createVerticalBox();
        jScrollPane = new JScrollPane();
        jScrollPane.setPreferredSize(new Dimension(1000,400));
        JPanel jPanelTableHeader = new JPanel();

        JLabel labelBarCode = new JLabel("条形码");
        labelBarCode.setPreferredSize(new Dimension(200, 20));
        labelBarCode.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelBarCode);

        JLabel labelBroadName = new JLabel("商品名称");
        labelBroadName.setPreferredSize(new Dimension(200, 20));
        labelBroadName.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelBroadName);

        JLabel labelgdNum = new JLabel("数量");
        labelgdNum.setPreferredSize(new Dimension(200, 20));
        labelgdNum.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelgdNum);

        JLabel labelgdPrice = new JLabel("交易编码");
        labelgdPrice.setPreferredSize(new Dimension(200, 20));
        labelgdPrice.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelgdPrice);

        jScrollPane.setColumnHeaderView(jPanelTableHeader);

        listModelArray = new ListModelArray<Detailed>();
        listModelArray.setList(new ArrayList<Detailed>());

        jList = new JList<Detailed>(listModelArray);
        jScrollPane.setViewportView(jList);
        verticalBox.add(jScrollPane);

        returnGoodsListPanel = new JPanel();
        returnGoodsListPanel.add(verticalBox);
    }

    public void flush(Detailed detailed){
        if(listModelArray.getSize() <= 0){
            listModelArray.addList(detailed);
        }else if(!isContains(listModelArray,detailed)){
            listModelArray.addList(detailed);
        }

        jList.setCellRenderer(new ListCellRenderer<Detailed>() {
            @Override
            public Component getListCellRendererComponent(JList<? extends Detailed> list, Detailed value, int index, boolean isSelected, boolean cellHasFocus) {

                JPanel jPanelTableHeader = new JPanel();

                if(cellHasFocus){
                    jPanelTableHeader.setBackground(Color.RED);
                }
                else if(isSelected){
                    jPanelTableHeader.setBackground(Color.BLUE);
                }else{
                    jPanelTableHeader.setBackground(Color.white);
                }

                JLabel labelBarCode = new JLabel(value.getGdBarCode());
                labelBarCode.setHorizontalAlignment(SwingConstants.CENTER);
                labelBarCode.setPreferredSize(new Dimension(200, 15));
                labelBarCode.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelBarCode);

                JLabel labelBroadName = new JLabel(value.getGdBroadName());
                labelBroadName.setHorizontalAlignment(SwingConstants.CENTER);
                labelBroadName.setPreferredSize(new Dimension(200, 15));
                labelBroadName.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelBroadName);

                JLabel labelgdNum = new JLabel(String.valueOf(value.getDtgdNum()));
                labelgdNum.setHorizontalAlignment(SwingConstants.CENTER);
                labelgdNum.setPreferredSize(new Dimension(200, 15));
                labelgdNum.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelgdNum);

                JLabel labelgdPrice = new JLabel(value.getTrUuid());
                labelgdPrice.setHorizontalAlignment(SwingConstants.CENTER);
                labelgdPrice.setPreferredSize(new Dimension(200, 15));
                labelgdPrice.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelgdPrice);

                return jPanelTableHeader;
            }
        });
    }

    public void Clear() {
        ListModelArray listModelArray1 = (ListModelArray) jList.getModel();
        listModelArray1.removeAllElements();
        listModelArray1.clearList();
        jList.repaint();
        jList.validate();
    }
    
    public boolean isContains(ListModelArray<Detailed> listModelArray,Detailed detailed){
        List<Detailed> tmplist = new ArrayList<Detailed>();
        tmplist = listModelArray.getList();
        for(int i=0;i<listModelArray.getSize();++i){
            Detailed tmpdetailed = tmplist.get(i);
            if(tmpdetailed.getTrUuid().equals(detailed.getTrUuid())){
                return true;
            }
        }
        return false;
    }
    
    public JPanel getReturnGoodsListPanel(){
        return returnGoodsListPanel;
    }

    public ListModelArray<Detailed> getListModelArray(){
        return this.listModelArray;
    }

    public static synchronized ReturnGoodsList getReturnGoodsList(){
        if(returnGoodsList == null){
            returnGoodsList = new ReturnGoodsList();
        }
        return returnGoodsList;
    }
}
